package com.neusoft.tijiancms.service.impl;

import com.neusoft.tijiancms.mapper.*;
import com.neusoft.tijiancms.po.*;
import com.neusoft.tijiancms.service.CireportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CireportServiceImpl implements CireportService {
    @Autowired
    private CireportMapper cireportMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private CidetailedreportMapper cidetailedreportMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Transactional
    @Override
    public int createReportTemplate(Orders orders) {
        //检查当前预约编号所对应的体检报告模板是否已经生成
        int num = cireportMapper.getCireportById(orders.getOrderId());
        if (num > 0) {
            return 1;
        }
        //查询要生成报告的模板信息（关联查询4张表）
        Setmeal setmeal = setmealMapper.getSetmealById(orders.getSmId());
        //将第2步中查询的数据向cireport表中插入数据
        List<Cireport> list1 = new ArrayList<>();
        for (Setmealdetailed sd : setmeal.getList()) {
            Cireport cireport = new Cireport();
            cireport.setCiId(sd.getCheckitem().getCiId());
            cireport.setCiName(sd.getCheckitem().getCiName());
            cireport.setOrderId(orders.getOrderId());
            list1.add(cireport);
        }
        int result1 = cireportMapper.insertCireport(list1);
        //将查询到的信息向Cidetailedreport中插入数据（批量添加）
        List<Cidetailedreport> list2 = new ArrayList<>();
        for (Setmealdetailed sd : setmeal.getList()) {
            for (Checkitemdetailed cid : sd.getCheckitem().getCheckitemdetailedList()) {
                Cidetailedreport cidetailedreport = new Cidetailedreport();
                cidetailedreport.setName(cid.getName());
                cidetailedreport.setUnit(cid.getUnit());
                cidetailedreport.setMinrange(cid.getMinrange());
                cidetailedreport.setMaxrange(cid.getMaxrange());
                cidetailedreport.setNormalValue(cid.getNormalValue());
                cidetailedreport.setNormalValueString(cid.getNormalValueString());
                cidetailedreport.setType(cid.getType());
                cidetailedreport.setValue("");
                cidetailedreport.setIsError(0);
                cidetailedreport.setCiId(sd.getCiId());
                cidetailedreport.setOrderId(orders.getOrderId());
                list2.add(cidetailedreport);
            }
        }
        int result2 = cidetailedreportMapper.insertCidetailedreport(list2);

        return result1 > 0 && result2 > 0 ? 1 : 0;
    }

    @Override
    public List<Cireport> listCireport(Integer orderId) {
        //先查询Cireport获取体检报告中的检查项
        List<Cireport>list=cireportMapper.listCireport(orderId);
        //根据上面查询获取的检查项，再查询检查项明细
        Cidetailedreport cidetailedreport=new Cidetailedreport();
         cidetailedreport.setOrderId(orderId);
       cidetailedreportMapper.getCidetailedreportById(cidetailedreport);
        return list;
    }


}
